package com.asion.vmovie.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.asion.vmovie.R;
import com.asion.vmovie.beans.DetialsBean;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by asion on 16-10-20.
 */

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.MyHolder> {

    private Context context;
    private List<DetialsBean.DataBean> list;
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public SearchAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.muhou_recycler_item, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        DetialsBean.DataBean dataBean = list.get(position);
        Glide.with(context).load(dataBean.getImage()).asBitmap().error(R.mipmap.default_error).into(holder.image);
        holder.title.setText(dataBean.getTitle());
        holder.sharecount.setText(dataBean.getShare_num());
        holder.likecount.setText(dataBean.getLike_num());
        holder.itemView.setOnClickListener(v->onItemClickListener.onItemClick(dataBean));
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class MyHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.image)
        ImageView image;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.sharecount)
        TextView sharecount;
        @BindView(R.id.likecount)
        TextView likecount;

        public MyHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public void addAll(Collection collection) {
        list.addAll(collection);
        notifyDataSetChanged();
    }

    public void removeAll() {
        list.clear();
        notifyDataSetChanged();
    }

    public interface OnItemClickListener{
        void onItemClick(DetialsBean.DataBean dataBean);
    }
}
